//
//  HomeShowPage.swift
//  FlameB
//
//  Created by 连俊 on 2020/8/5.
//  Copyright © 2020 连俊. All rights reserved.
//

import UIKit
import Charts

class HomeShowPage: BaseViewController {
    
    var chartView: BarChartView!
    
    var dataSource: [StoreIMBrand] = [StoreIMBrand]()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "100m柱状图"
        
        chartView = BarChartView()
        chartView.xAxis.valueFormatter = self
        chartView.frame = CGRect(x: 0, y: navigationBarHeight, width: UIScreen.appWidth(),
                                 height: UIScreen.appHeight()-navigationBarHeight)
        view.addSubview(chartView)
        
        chartView.noDataText = "暂无数据"
        
        var dataEntries = [BarChartDataEntry]()
        let arr = DBService.shared.loadAllBrands()
        dataSource = arr
        for (idx, model) in arr.enumerated() {
            let dataEntry = BarChartDataEntry()
            dataEntry.x = Double(idx)
            dataEntry.y = Double(model.totalTime)
            dataEntries.append(dataEntry)
        }
        let charDataSet = BarChartDataSet(entries: dataEntries, label: "图例")
        let chartData = BarChartData(dataSets: [charDataSet])
        chartView.data = chartData
        chartView.xAxis.labelCount = arr.count
    }
}

extension HomeShowPage: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        if Int(value) < dataSource.count {
            return dataSource[Int(value)].lastMsgTime
        }
        return ""
    }
}




























